package site.lyweb.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import site.lyweb.domain.dto.PageDTO;
import site.lyweb.domain.dto.Student;
import site.lyweb.domain.dto.StudentDTO;
import site.lyweb.domain.dto.TeacherDTO;
import site.lyweb.domain.query.StudentQuery;
import site.lyweb.domain.query.TeacherQuery;
import site.lyweb.result.RestResult;
import site.lyweb.service.StudentService;
import site.lyweb.utils.UUIDUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Author 林瑶
 * @Description TODO
 * @Date 2024/6/19 21:02
 */
@CrossOrigin
@RestController
@Tag(name="学生管理模块")
@Slf4j
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;

    /**
     * 根据id查找学生信息表中信息
     */
    @Operation(summary = "根据id查找学生信息表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @GetMapping("/stu_info/getById/{id}")
    public RestResult queryStudentById(@PathVariable("id") String id){
        Student byId = studentService.getById(id);

        return RestResult.SUCCESS(byId);
    }
    /**
     * 查找全部学生信息表中信息
     */
    @Operation(summary = "查找全部学生信息表中信息")
    @GetMapping("/stu_info/getAll")
    public RestResult queryStudentAll(){
        return RestResult.SUCCESS(studentService.list());
    }
    /**
     * 根据id删除学生信息表中信息
     */
    @Operation(summary = "根据id删除学生信息表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @PostMapping("/stu_info/del/{id}")
    public RestResult delStudentById(@PathVariable("id") String id){
        return studentService.removeById(id) ?RestResult.SUCCESS():RestResult.FAIL();
    }

    /**
     * 保存学生信息表中信息
     */
    @Operation(summary = "保存学生信息表中信息")
    @PostMapping("/stu_info/edit")
    @Parameters({
            @Parameter(name = "id",description = "id（主键）",in = ParameterIn.QUERY),
            @Parameter(name = "name",description = "姓名",in = ParameterIn.QUERY),
            @Parameter(name = "age",description = "年龄",in = ParameterIn.QUERY),
            @Parameter(name = "phone",description = "手机号",in = ParameterIn.QUERY),
    })
    public RestResult editStudent(Student student){
        if(student!=null){
            //有id：修改，无id:添加
            if(student.getId()!=null && !student.getId().isEmpty()){
                return studentService.updateById(student)?RestResult.SUCCESS("修改成功："+student):RestResult.FAIL();
            }else{
                student.setId(UUIDUtils.getUUID());
                return studentService.save(student)?RestResult.SUCCESS("添加成功："+student):RestResult.FAIL();
            }
        }
        return RestResult.FAIL("传入学生对象为空，保存失败");
    }
    /**
     * 分页查询学生列表数据
     *
     * @return
     */

    @Operation(summary = "分页查询学生列表数据")
    @GetMapping(value = "/stu_info/getStudentByPage")
    public RestResult getStudentByPage( StudentQuery studentQuery) {


        //分页查询的结果用PageDTO进行封装
        PageDTO<StudentDTO> pageDTO = studentService.getStudentByPage(0, studentQuery);

        //有的时候，查询可以不判断成功与失败，不管查没查到数据，都返回200，页面上去控制显示
        return RestResult.SUCCESS(pageDTO);
    }
    /**
     * 根据id批量学生信息表中信息
     */
    @Operation(summary = "根据ids删除学生信息表中信息")
    @Parameters({
            @Parameter(name = "ids",description = "id(主键)",in = ParameterIn.PATH)
    })
    @CrossOrigin
    @PostMapping("/stu_info/batchDel/{ids}")
    public RestResult batchDelStudentById(@PathVariable("ids") String ids){
        //ids = "1,2,5,7,10,12" , ids.split(",")是把ids转成数组, Arrays.asList()是把数组转成List
        List<String> idList = Arrays.asList(ids.split(","));

        return  studentService.removeBatchByIds(idList) ? RestResult.SUCCESS() : RestResult.FAIL();
    }


}
